Features
|
Install
|
Usage
|
Examples
About
Parsec is a tiny CLI parser.
Features
- Default values / types
- Handle
--no-*
options - Handle unknown options
Install
npm install parsec
Usage
parse()
{
"p": true,
"a": true,
"r": "5ec"
}
Customize:
parse("secret", ["verbose", "V", { default: true }])
{
"s": 42,
"secret": 42,
"V": true,
"verbose": true
}
Usage
Parse process.argv
by default.
import parse from "parsec"
parse(alias1, alias2, ...)
parse(["foo", "bar", "baz"])
{
"foo": true,
"bar": true,
"baz": true
}
"foo"
["F", "f", "foo"]
["foo", { default: "./" }]
["baz", { default: true }]
parse("foo", "bar")
{
"f": true,
"foo": true,
"b": true,
"bar": true,
}
parse(["f", "file", { default: "." }])
{
"f": ".",
"file": "."
}
parse()
{
"foo": false,
"no-bar": "baz"
}
parse("foo", (option) => {
throw new RangeError(`unknown option ${option}`)
})
- Bare operands and arguments after
--
are added to ._
. To override:
parse(["_", "alias"])
- Bind
parse
to a different source of arguments:
parse.call(["--foo", "--bar"], [alias1, alias2, ...])
Examples
parse()
{
"f": "bar",
"b": true,
"p": "./"
}
with custom aliases:
parse("foo", "bar", ["path", { default: "./" }])
{
"f": "bar",
"foo": "bar",
"b": true,
"bar": true,
"p": "./",
"path": "./"
}